package com.example.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.example.config.CustomConfig;
import com.example.dao.SlowSqlInfo;
import com.example.util.HttpRequest;
import com.example.util.LogUtil;
import org.springframework.stereotype.Component;

@Component
public class SlowSqlChecker {

    private final CustomConfig customConfig;

    public SlowSqlChecker(CustomConfig customConfig) {
        this.customConfig = customConfig;
    }

    public void check(SlowSqlInfo slowSqlInfo) {
        try {
//            System.out.println("slowSqlInfo.getQueryCount(): " + slowSqlInfo.getQueryCount());
//            System.out.println("customConfig.getQueryCountThreshold()：" + customConfig.getQueryCountThreshold());
//            System.out.println("slowSqlInfo.getQueryTimeAvg()：" + slowSqlInfo.getQueryTimeAvg());
//            System.out.println("customConfig.getQueryTimeAvg(): " + customConfig.getQueryTimeAvg());
            if ((Double.parseDouble(slowSqlInfo.getQueryCount()) < Double.parseDouble(customConfig.getQueryCountThreshold()))
                    & (Double.parseDouble(slowSqlInfo.getQueryTimeAvg())) < Double.parseDouble(customConfig.getQueryTimeAvg())) {
                return;
            }

            JSONObject alertObj = assembleAlertObj(slowSqlInfo);

            String notificationUrl = customConfig.getNotificationUrl();
            String headers = "";
            String jsonInputString = alertObj.toString();
            LogUtil.logInfo("告警内容：" + jsonInputString);
            String response = HttpRequest.sendPost(notificationUrl, headers, jsonInputString, true);
            LogUtil.logInfo("response: " + response);
        } catch (Exception e) {
            LogUtil.logError(e.toString(), e);
            LogUtil.logInfo(JSON.toJSONString(slowSqlInfo));
            LogUtil.logInfo(JSON.toJSONString(new CustomConfig()));
        }
    }

    private static JSONObject assembleAlertObj(SlowSqlInfo slowSqlInfo) {
        String description_str = String.format("主机 %s，数据库 %s, 端口 %s, 客户端IP %s, 执行SQL %s, 共 %s 次, 平均用时 %s 秒,首次执行时间 %s, 最后一次执行时间 %s ,源日志路径 %s",
                slowSqlInfo.getHost(), slowSqlInfo.getDb(), slowSqlInfo.getPort(), slowSqlInfo.getClientHost(), slowSqlInfo.getFingerprint(),
                slowSqlInfo.getQueryCount(), slowSqlInfo.getQueryTimeAvg(), slowSqlInfo.getTsMin(),
                slowSqlInfo.getTsMax(), slowSqlInfo.getRawFilePath());
        JSONObject alert = new JSONObject();
        JSONObject labels = new JSONObject();
        labels.put("alertname", "有慢SQL产生");
        labels.put("paasServiceName", "Teledb");
        labels.put("severity", "critical");
        alert.put("labels", labels);
        JSONObject annotations = new JSONObject();
        annotations.put("summary", "有慢SQL产生");
        annotations.put("description", description_str);
        alert.put("annotations", annotations);
        alert.put("status", "firing");
        JSONArray alertsJson = new JSONArray();
        alertsJson.add(alert);
        JSONObject alertObj = new JSONObject();
        alertObj.put("status", "firing");
        alertObj.put("alerts", alertsJson);
        return alertObj;
    }

}
